package com.wondertek.applet.controller.business;

import com.github.pagehelper.PageInfo;
import com.wondertek.common.base.Result;
import com.wondertek.common.log.AutoLogUtil;
import com.wondertek.service.dto.app.req.AppDevicePageByUserIdReq;
import com.wondertek.service.dto.app.res.AppDeviceNumByUserIdRes;
import com.wondertek.service.dto.app.res.AppDevicePageByUserIdRes;
import com.wondertek.service.dto.business.res.SiteDeviceAccessoryRes;
import com.wondertek.service.entity.business.DevicePo;
import com.wondertek.service.sdk.iotdh.dto.req.*;
import com.wondertek.service.sdk.iotdh.dto.res.GetStatusRes;
import com.wondertek.service.sdk.iotdh.dto.res.LocalRecordRes;
import com.wondertek.service.sdk.iotdh.service.DhDeviceService;
import com.wondertek.service.service.business.DeviceService;
import com.wondertek.service.service.business.RentalService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 设备管理
 * @author: qinjianhui
 * @create: 2024-08-01 10:13
 **/
@RestController
@RequestMapping("/app/business/device/")
@Tag(name = "设备管理")
@CrossOrigin("*")
public class DeviceController {

    @Resource
    private DeviceService deviceService;

    @Resource
    private DhDeviceService dhDeviceService;

    @Resource
    private RentalService rentalService;

    @PostMapping("/pageByUserId")
    @Operation(summary = "查询设备")
    public Result<PageInfo<AppDevicePageByUserIdRes>> pageByUserId(@RequestBody AppDevicePageByUserIdReq req){
        return Result.success(deviceService.pageByUserId(req));
    }

    @PostMapping("/numByUserId")
    @Operation(summary = "查询设备数量")
    public Result<AppDeviceNumByUserIdRes> numByUserId(@RequestBody AppDevicePageByUserIdReq req){
        return Result.success(deviceService.numByUserId(req));
    }

    @PostMapping("/ipc/detail")
    @Operation(summary = "查询ipc设备详情")
    public Result<String> ipcDetail(@RequestBody @Validated StreamUrlAddDhReq req){
        return dhDeviceService.addStreamUrl(req);
    }

    @PostMapping("/ipc/localRecords")
    @Operation(summary = "查询ipc回放列表")
    public Result<List<LocalRecordRes>> ipcLocalRecords(@RequestBody @Validated LocalRecordsReq req){
        Result<List<LocalRecordRes>> res = dhDeviceService.LocalRecords(req);
        List<LocalRecordRes> list = res.getData();
        List<LocalRecordRes> list1 = getLocalRecords(list,req.getEndTime(),req.getDeviceSn());
        return Result.success(list1);
    }

    private List<LocalRecordRes> getLocalRecords(List<LocalRecordRes> list, String endTime, String deviceSn){
        if(CollectionUtils.isNotEmpty(list) && (list.size() % 64 == 0 || list.size() % 63==1)){
            String startTime = list.get(list.size()-1).getEndTime();
            LocalRecordsReq req = new LocalRecordsReq();
            req.setBeginTime(startTime);
            req.setEndTime(endTime);
            req.setCount("64");
            req.setDeviceSn(deviceSn);
            Result<List<LocalRecordRes>> res = dhDeviceService.LocalRecords(req);
            List<LocalRecordRes> list1 = res.getData();
            if(CollectionUtils.isNotEmpty(list1)){
                list.remove(list.size()-1);
                list.addAll(list1);
                this.getLocalRecords(list,endTime,deviceSn);
            }
        }
        return list;
    }

    @PostMapping("/network/set/detail")
    @Operation(summary = "查询网关设备开关详情")
    public Result<?> setDetail(@RequestBody @Validated GetEnableReq req){
        return dhDeviceService.getEnable(req);
    }

    @PostMapping("/network/set")
    @Operation(summary = "网关开关设置")
    public Result<?> set(@RequestBody @Validated SetEnableReq req){
        DevicePo devicePo = new DevicePo();
        devicePo.setSnCode(req.getDeviceId());
        SiteDeviceAccessoryRes res = deviceService.selectSiteDevice(devicePo);
        AutoLogUtil.save("sys","log.gateway.switch.set","网关sn码:"+req.getDeviceId(),
                res.getSiteName(),res.getDeviceSn(),res.getSiteId(),null);
        return dhDeviceService.setEnable(req);
    }

    @PostMapping("/network/detail")
    @Operation(summary = "查询网关设备详情")
    public Result<GetStatusRes> networkDetail(@RequestBody @Validated GetStatusReq req){
        Result<GetStatusRes> res = dhDeviceService.getStatus(req);
        return res;
    }

}

